package cc.wsyw126.java.nowcoder.sword_offer;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/14.
 */
public class GetUglyNumber_Solution {
    private ArrayList<Integer> result = new ArrayList<>();
    {
        result.add(1);
    }
    public int GetUglyNumber_Solution(int index) {
        if (index <= 0) return 0;
        if (index <= result.size()) return result.get(index-1);
        while (index>result.size()) {
            int size = result.size();
            int min = Integer.MAX_VALUE;
            int i = result.get(size-1) / 2;
            for (int j = 0; j < size; j++) {
                Integer integer = result.get(j);
//                if (integer >= i) {
                    int i2 = integer * 2;
                    int i3 = integer * 3;
                    int i5 = integer * 5;
                    if (i2<min && i2 > result.get(size-1)) {
                        min=i2;
                    }
                    if (i3<min && i3 > result.get(size-1)) {
                        min=i3;
                    }
                    if (i5<min && i5 > result.get(size-1)) {
                        min=i5;
                    }
//                }
            }
            result.add(min);
        }
        return result.get(result.size()-1);
    }

    public static void main(String[] args) {
        GetUglyNumber_Solution getUglyNumber_solution = new GetUglyNumber_Solution();
        int i = getUglyNumber_solution.GetUglyNumber_Solution(5);
        System.out.println("i = " + i);
    }
}
